/*
Copyright 2010-2011 CumulusCloud
http://cumuluscloud.cc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cumulusCloud.knowledge;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

import cumulusCloud.common.objects.CloudUser;

public class UserKnow {
	static private Logger logger = java.util.logging.Logger.getLogger(UserKnow.class.getName());	
	static private UserKnow instance;
	
	public static UserKnow getInstance() {
		if (instance == null) instance = new UserKnow();
		return instance;
	}
	
	private Map<String, CloudUser> users = new ConcurrentHashMap<String, CloudUser>();
	
	private UserKnow() {
		String[] names = {"testing", "alpha", "beta", "gamma", "hercule"};
		
		for (String name : names) {
			CloudUser user = new CloudUser(name, name, "EN", true);
			users.put(name, user);
		}
		
		users.put("notadmin", new CloudUser("notadmin", "notadmin", "EN", false));
		users.put("french", new CloudUser("french", "french", "FR", true));
	}
	
	public synchronized boolean createUser(CloudUser cu) {		
		logger.info("Creating user: " + cu);
		
		if (getUser(cu.name) != null) {
			logger.info("The user " + cu.name + " already exists");
			return false;
		}
		
		logger.info("The user " + cu.name + " has been created");
		users.put(cu.name, cu);
		
		return true;
	}
	
	public synchronized boolean deleteUser(String user) {
		logger.info("Deleting user: " + user);
		
		CloudUser cu = users.remove(user);
		
		return cu!=null;
	}
	
	public CloudUser getUser(String user) {
		logger.info("Getting info for user " + user);
		CloudUser resultUser = users.get(user);

		if (resultUser == null) {
			logger.info("User " + user + " not found");
			return null;
		}
		
		return new CloudUser(resultUser);
	}
	
	public boolean isValidCredential(String user, String password) {	
		logger.info("Validating credential user: " + user + " | " + password);
		
		if (password == null) {
			logger.severe("No password");
			return false;
		}
		
		CloudUser dbUser = getUser(user);
		if (dbUser == null) {
			logger.info("The user does not exists: " + user);
			return false;
		}
		
		if (password.equals(dbUser.password)) {
			logger.info("The password is good for " + user);
			return true;
		}
		
		logger.info("Wrong password for " + user);
		return false;
	}
	
	public CloudUser[] listUsers() {
		CloudUser[] result = new CloudUser[users.size()];
		
		int i=0;
		for (CloudUser cu: users.values()) {
			result[i++] = new CloudUser(cu);
		}
		
		return result;
	}

	public boolean modifyUser(CloudUser previous, CloudUser modified) {
		logger.info("Modifying user " + previous.name);
		
		// Check if user exists
		if (users.get(previous.name) == null) {
			logger.warning("The user " + previous.name + " does not exists");
			return false;
		}
		
		// Update
		users.put(modified.name, modified);
		if (!previous.name.equals(modified.name)) {
			users.remove(previous.name);
		}
		
		return true;
	}
}
